#ifndef WINDOWS
#define WINDOWS

#include <stdio.h>
#include "lib/ArrayList.h"

class Window;
class Windows;

//void recursiveProc(Window* parent, Window* self, void* param);

class Window{
  void (*pMouseDown)(Window* self, int x, int y, int e);
  void (*pMouseUp)(Window* self, int x, int y, int e);
  void (*pMouseMove)(Window* self, int x, int y, int e);
  void (*pKeyEvent)(Window* self, int key, int chr);
  void (*pDraw)(Window*);
  void (*pDispose)(Window*);

  public:
  void init();

  void regMouseDown(void (p)(Window*, int, int, int));
  void regMouseUp(void (p)(Window*, int, int, int));
  void regMouseMove(void (p)(Window*, int, int, int));
  void regKeyEvent(void (p)(Window*, int, int));
  void regDraw(void (p)(Window*));
  void regDispose(void (p)(Window*));

  void mouseDown(int x, int y, int e);
  void mouseUp(int x, int y, int e);
  void mouseMove(int x, int y, int e);
  void keyEvent(int key, int chr);
  void draw();
  void dispose();

  void addChild(Window* childWindow);
  void recursiveChild(Window* parent, int topDown, void* param, void (recursiveProc)(Window* parent, Window* self, void* param));
  
  FILE* getStream(int x, int y, int w, int h);// const char* str,
  void paintWin(int, int, int, int, int, int);
  void drawImgWin(void*, int, int, int, int);

  int printX;
  int printY;
  int printBaseX;
  int printBaseY;
  int printWidth;
  int printHeight;
  int printColor;
  int id;
  int pos_x;
  int pos_y;
  int abs_pos_x;
  int abs_pos_y;
  int width;
  int height;
  int color;
  int bg;
  int zIndex;
  int needRefresh;
  FILE stream;
  ArrayList* children;
  Window* parent;
  Window* topForm;
  Windows* windows;
};

class Button : public Window{
  void (*pClick)(Button* self);
  void (*pDraw)(Button* self);
  public:
  void init(char* label);
  void regClick(void (p)(Button*));
  void regDraw(void (p)(Button*));
  
  static void mouseDown(Window*, int x, int y, int e);
  static void mouseUp(Window*, int x, int y, int e);
  static void draw(Window*);
  
  char* label;
  int textX;
  int textY;
  int isDown;
};

class Form : public Window{
  void (*pMouseDown)(Form* self, int x, int y, int e);
  void (*pMouseUp)(Form* self, int x, int y, int e);
  void (*pDraw)(Form*);
  void (*pClose)(Form*);

  public:
  void init(char* title);

  void regMouseDown(void (p)(Form*, int, int, int));
  void regMouseUp(void (p)(Form*, int, int, int));
  void regDraw(void (p)(Form*));
  void regClose(void (p)(Form*));

  static void close(Button*);
  
  static void mouseDown(Window*, int x, int y, int e);
  static void mouseUp(Window*, int x, int y, int e);
  static void draw(Window*);

  char* title;
  Button* buttonClose;

  int move;
  int mouseDownX;
  int mouseDownY;
};

// class ScrollBar : public Window{
//   public:
//   int max;
//   int min;
//   int pageSize;
//   int currVal;
//   int style;//0:H 1:V
//   void (*pOnChange)(ScrollBar* self, int currVal);
//   void init(int style);
//   void dispose();
//   void draw();
//   void mouseDown(int x, int y, int e);
//   void mouseUp(int x, int y, int e);
//   void mouseMove(int x, int y, int e);
//   void keyEvent(int key, int chr);
//   Button* buttonInc;
//   Button* buttonDec;
//   void dec();
//   void inc();
//   void getPos(int* pos, int* barSize);
// };

// class ListItem{
//   public:
//   char* text;
//   void* obj;
// };

// class List : public Window{
//   public:
//   void (*pOnSelect)(ListItem*);
//   int selectedIndex;
//   ArrayList* items;//ListItem
//   void init();
//   void dispose();
//   void draw();
//   void mouseDown(int x, int y, int e);
//   void mouseUp(int x, int y, int e);
//   void mouseMove(int x, int y, int e);
//   void keyEvent(int key, int chr);
//   void addItem(char* text, void* obj);
  
//   void onScrollBarChange(int val);
  
//   ScrollBar* scrollBar;
// };

class Timer_win{
  public:
  int interval;//ms
  int nextTick;//ms
  void (*pOnTime)(Timer_win*);
  void* object;
};

class Windows{
  public:
  int width;
  int height;
  Window* topChild;
  Window* topWindow;

  Window* zIndexCache[256];//针对topWindow 的，topwindow更换时要重算
  int     zIndexCacheIndex;
  Window* mouseDownObj;
  int oldbtn1;
  int oldbtn2;
  int mousex;
  int mousey;
  int screenBase;
  void init(int, int, int, int*, int*);
  int needRefresh;
  void start();
  ArrayList* windows;
  ArrayList* timers;
  
  void addTimer(Timer_win* timer);
  void removeTimer(Timer_win* timer);
  void addWindow(Window* window);
  void removeWindow(Window* window);
  void setTopWindow(Window* window);
  void refresh();
  void doRefresh();
  void mouseEvent(int btn,int state);
  void keyEvent(int key, int chr);
  //display
  
  //主循环
  
  //定时器中断
  
  //mouse event
  
  //key event
};

#endif
